﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using SocialNetwork.Domain;
using SocialNetwork.Domain.Repositories.Interfaces;
using SocialNetwork.Models;

namespace SocialNetwork.Controllers
{
    public class HomeController : Controller
    {
        private IScheduleRepository scheduleRepository;

        public HomeController(IScheduleRepository scheduleRepository)
        {
            this.scheduleRepository = scheduleRepository;
        }

        public ActionResult Index()
        {
            Database.SetInitializer<SocialNetworkContext>(null);
            var scheduleDate = new DateTime(2012, 1, 1);
            var schedules = scheduleRepository.GetSchedules(scheduleDate);
            var schedulesGroupsByCinems = schedules.GroupBy(s => s.Cinema);

            var scheduleViewModel = new ScheduleViewModel
                                        {
                                            Date = scheduleDate
                                        };

            foreach (var cinemaGroup in schedulesGroupsByCinems)
            {
                var cinemaSchedule = new CinemaSchedule {CinemaName = cinemaGroup.Key.Name};
                foreach (var schedule in cinemaGroup)
                {
                    var movieSchedule = new MovieSchedule { MovieName = schedule.Movie.Name, Sessions = schedule.Sessions};
                    cinemaSchedule.Movies.Add(movieSchedule);
                }
                scheduleViewModel.CinemaSchedules.Add(cinemaSchedule);
            }
            return View(scheduleViewModel);
        }
    }
}